package stark.app.function.catdog;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.catdog.lib.CatDogResProvider;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import stark.app.R;
import stark.app.databinding.ActivityCatDogListBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.constant.Extra;
import stark.common.basic.utils.IntentUtil;

/**
 * Author：daizhongbin on 2022/8/31
 * Email：1027154338@qq.com
 * Desc:
 */
public class CatDogListActivity extends BaseNoModelActivity<ActivityCatDogListBinding> {

    private int mType = CatDogAdapter.TYPE_CAT;
    private CatDogAdapter mCatDogAdapter;

    public static void showCat(Context context) {
        Intent intent = IntentUtil.getIntent(context, CatDogListActivity.class);
        intent.putExtra(Extra.TYPE, CatDogAdapter.TYPE_CAT);
        context.startActivity(intent);
    }

    public static void showDog(Context context) {
        Intent intent = IntentUtil.getIntent(context, CatDogListActivity.class);
        intent.putExtra(Extra.TYPE, CatDogAdapter.TYPE_DOG);
        context.startActivity(intent);
    }

    @Override
    protected int onCreate() {
        return R.layout.activity_cat_dog_list;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            mType = intent.getIntExtra(Extra.TYPE, CatDogAdapter.TYPE_CAT);
        }

        //rvCat
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        mDataBinding.rvContainer.setLayoutManager(layoutManager);
        CatDogAdapter catDogAdapter = new CatDogAdapter(mType);
        mCatDogAdapter = catDogAdapter;
        catDogAdapter.setOnItemClickListener(this);
        if (mType == CatDogAdapter.TYPE_CAT) {
            catDogAdapter.setNewInstance(CatDogResProvider.getCatSounds());
        } else {
            catDogAdapter.setNewInstance(CatDogResProvider.getDogSounds());
        }
        mDataBinding.rvContainer.setAdapter(catDogAdapter);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopPlaySound();
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (mPlayingPos == position) {
            mPlayingPos = -1;
            stopPlaySound();
            mCatDogAdapter.setPlayingPos(-1);
        } else {
            startPlaySound(mCatDogAdapter.getItem(position).getSoundPath());
            mCatDogAdapter.setPlayingPos(position);
            mPlayingPos = position;
        }
    }

    private MediaPlayer mMediaPlayer;

    private int mPlayingPos = -1;

    private void startPlaySound(String path) {
        stopPlaySound();

        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                }
            });
            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mPlayingPos = -1;
                    mCatDogAdapter.setPlayingPos(-1);
                }
            });
        }

        try {
            AssetFileDescriptor afd = getAssets().openFd(path);
            mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            mMediaPlayer.prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void stopPlaySound() {
        if (mMediaPlayer != null) {
            try {
                mMediaPlayer.stop();
                mMediaPlayer.release();
                mMediaPlayer = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
